<template>
  <header :class="['header', ,'py-4',{ 'scrolled': isScrolled }]">
    <div class="container py-4">
      <div class="header-content">
        <!-- Logo -->
        <NuxtLink to="/" class="logo">
          <img class="logo-icon" src="/logo.png" alt="个人博客" />
          <span class="logo-text">个人博客</span>
        </NuxtLink>

        <!-- 桌面导航 -->
        <nav class="desktop-nav">
          <a 
            v-for="item in menuItems" 
            :key="item.id"
            :href="item.path"
            :class="['nav-link', { 'resume-link': item.title === '简历', 'home-link': item.title === '首页' }]"
            @click.prevent="handleNavClick(item)"
          >
            <Icon v-if="item.title === '首页'" name="fa:home" class="home-icon" />
            <Icon v-if="item.title === '简历'" name="fa:file-text" class="resume-icon" />
            {{ item.title }}
          </a>
        </nav>

        <!-- 移动端菜单按钮 -->
        <button 
          class="mobile-menu-btn"
          @click="toggleMobileMenu"
          aria-label="Toggle menu"
        >
          <Icon v-if="!mobileMenuOpen" name="fa:bars" />
          <Icon v-else name="fa:times" />
        </button>
      </div>

      <!-- 移动端导航菜单 -->
      <Transition name="mobile-menu">
        <div v-if="mobileMenuOpen" class="mobile-menu">
          <a 
            v-for="item in menuItems" 
            :key="item.id"
            :href="item.path"
            :class="['mobile-nav-link', { 'mobile-resume-link': item.title === '简历', 'mobile-home-link': item.title === '首页' }]"
            @click.prevent="handleNavClick(item)"
          >
            <Icon v-if="item.title === '首页'" name="fa:home" class="mobile-home-icon" />
            <Icon v-if="item.title === '简历'" name="fa:file-text" class="mobile-resume-icon" />
            {{ item.title }}
          </a>
        </div>
      </Transition>
    </div>
  </header>
</template>

<script setup lang="ts">

import type { MenuItem } from '~/types'

// 移动端菜单状态
const mobileMenuOpen = ref(false)

// 路由检测
const route = useRoute()

// 根据当前路由动态生成菜单项
const menuItems = computed(() => {
  const isResumePage = route.path === '/resume'
  const isProjectPage = route.path.startsWith('/project/')
  
  if (isResumePage || isProjectPage) {
    // 在简历页面或项目详情页面时只显示首页按钮
    return [
      { id: '1', title: '首页', label: '首页', path: '/' },
    ]
  } else {
    // 在首页时显示所有内部导航和简历
    return [
      { id: '1', title: '关于我', label: '关于我', path: '#about' },
      { id: '2', title: '项目', label: '项目', path: '#projects' },
      { id: '3', title: '技能', label: '技能', path: '#skills' },
      { id: '4', title: '联系', label: '联系', path: '#contact' },
      { id: '5', title: '简历', label: '简历', path: '/resume' },
    ]
  }
})

// 切换移动端菜单
const toggleMobileMenu = () => {
  mobileMenuOpen.value = !mobileMenuOpen.value
}

// 关闭移动端菜单
const closeMobileMenu = () => {
  mobileMenuOpen.value = false
}

// 监听路由变化，关闭移动端菜单
watch(() => route.path, () => {
  mobileMenuOpen.value = false
})

// 滚动效果
const isScrolled = ref(false)

// 平滑滚动到锚点
const scrollToSection = (href: string) => {
  if (href.startsWith('#')) {
    const element: HTMLElement | null= document.querySelector(href)
    if (element) {
      const elementPosition = element.offsetTop
      const offsetPosition = elementPosition - 90
      
      window.scrollTo({
        top: offsetPosition,
        behavior: 'smooth'
      })
    }
  }
  closeMobileMenu()
}

// 处理导航链接点击
const handleNavClick = (item: MenuItem) => {
  if (item.path.startsWith('#')) {
    scrollToSection(item.path)
  } else {
    navigateTo(item.path)
    closeMobileMenu()
  }
}

onMounted(() => {
  const handleScroll = () => {
    isScrolled.value = window.scrollY > 50
  }
  
  window.addEventListener('scroll', handleScroll)
  
  onUnmounted(() => {
    window.removeEventListener('scroll', handleScroll)
  })
})
</script>

<style scoped>
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 50;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.header.scrolled {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  background: rgba(255, 255, 255, 0.95);
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 0;
  transition: all 0.3s ease;
}

.header.scrolled .header-content {
  padding: 0.5rem 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  text-decoration: none;
  color: var(--dark-color);
  font-weight: 600;
  font-size: 1.25rem;
}

.logo-icon {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 50%;
}

.header.scrolled .logo-icon {
  width: 2rem;
  height: 2rem;
}

.logo-text {
  color: var(--dark-color);
}

.desktop-nav {
  display: flex;
  align-items: center;
  gap: 2rem;
}

.nav-link {
  color: var(--muted-color);
  text-decoration: none;
  font-weight: 500;
  transition: var(--transition);
  position: relative;
}

.nav-link:hover,
.nav-link.active {
  color: var(--primary-color);
}

.nav-link::after {
  content: '';
  position: absolute;
  bottom: -0.5rem;
  left: 0;
  width: 0;
  height: 2px;
  background-color: var(--primary-color);
  transition: var(--transition);
}

.nav-link.active::after,
.nav-link:hover::after {
  width: 100%;
}

/* 简历导航特殊样式 */
.resume-link {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white !important;
  padding: 0.5rem 1rem;
  border-radius: 25px;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.resume-link:hover {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
  color: white !important;
}

.resume-link::after {
  display: none;
}

.resume-icon {
  font-size: 1rem;
  animation: pulse 2s infinite;
}

.home-icon {
  font-size: 1rem;
}

@keyframes pulse {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.7;
  }
}

.mobile-menu-btn {
  display: none;
  background: none;
  border: none;
  font-size: 1.25rem;
  color: var(--muted-color);
  cursor: pointer;
  transition: var(--transition);
}

.mobile-menu-btn:hover {
  color: var(--primary-color);
}

.mobile-menu {
  background: white;
  border-top: 1px solid var(--border-color);
  padding: 1rem 0;
}

.mobile-nav-link {
  display: block;
  padding: 0.75rem 0;
  color: var(--muted-color);
  text-decoration: none;
  font-weight: 500;
  transition: var(--transition);
}

.mobile-nav-link:hover {
  color: var(--primary-color);
}

/* 移动端简历导航特殊样式 */
.mobile-resume-link {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white !important;
  padding: 0.75rem 1rem;
  border-radius: 15px;
  margin: 0.5rem 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
  transition: all 0.3s ease;
}

.mobile-resume-link:hover {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
  transform: translateX(5px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
  color: white !important;
}

.mobile-resume-icon {
  font-size: 1rem;
  animation: pulse 2s infinite;
}

.mobile-home-icon {
  font-size: 1rem;
}

/* 首页导航特殊样式 */
.home-link {
  background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  color: white !important;
  padding: 0.5rem 1rem;
  border-radius: 25px;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(72, 187, 120, 0.3);
  transition: all 0.3s ease;
}

.home-link:hover {
  background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(72, 187, 120, 0.4);
  color: white !important;
}

.home-link::after {
  display: none;
}

/* 移动端首页导航特殊样式 */
.mobile-home-link {
  background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
  color: white !important;
  padding: 0.75rem 1rem;
  border-radius: 15px;
  margin: 0.5rem 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(72, 187, 120, 0.3);
  transition: all 0.3s ease;
}

.mobile-home-link:hover {
  background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
  transform: translateX(5px);
  box-shadow: 0 4px 12px rgba(72, 187, 120, 0.4);
  color: white !important;
}

/* 移动端样式 */
@media (max-width: 768px) {
  .desktop-nav {
    display: none;
  }
  
  .mobile-menu-btn {
    display: block;
  }
}

/* 移动端菜单动画 */
.mobile-menu-enter-active,
.mobile-menu-leave-active {
  transition: all 0.3s ease;
}

.mobile-menu-enter-from,
.mobile-menu-leave-to {
  opacity: 0;
  transform: translateY(-10px);
}
</style>